package com.ruoyi.web.controller.system.api.cc;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.interceptor.Authentication;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.dto.OfflineDto;
import com.ruoyi.system.api.service.OfflineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 离线功能
 *
 * @ClassName OfflineController.java
 * @date 2020/4/10 9:01 上午
 * @author yzg
 */
@Api(tags="离线功能")
@Controller
@RequestMapping("/api/cc/offline/")
public class OfflineController extends BaseController {

    @Autowired
    private OfflineService offlineService;

    private static final Logger log = LoggerFactory.getLogger(OfflineController.class);
    /**
     *  上传离线数据
     *
     * @param offlineDto
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/4/10 9:05 上午
     */
//    @Log(title = "上传离线数据", businessType = BusinessType.INSERT)
//    @PostMapping("uploadOfflineData")
//    @ResponseBody
//    @Authentication(tokenRequired = true, signRequired = true)
//    @ApiOperation(value = "上传离线数据", httpMethod = "POST", notes = "上传离线数据")
//    @ApiResponses({
//            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
//            @ApiResponse(code = 200, message = "code 为 200 成功")
//    })
//    public ResponseWithData<?> uploadOfflineData(@RequestBody OfflineDto offlineDto) {
//        try {
//       //     return offlineService.uploadOfflineData(offlineDto);
//        } catch (Exception e) {
//            log.error("程序异常", e.getMessage());
//            return new ResponseWithData().error().msg("系统异常").code(500);
//        }
//    }


    /**
     *  上传离线数据
     *
     * @param offlineDtoList
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/4/10 9:05 上午
     */
    @Log(title = "上传离线数据", businessType = BusinessType.INSERT)
    @PostMapping("uploadOfflineDataBatch")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "上传离线数据", httpMethod = "POST", notes = "上传离线数据")
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    @Transactional(rollbackFor = Exception.class)
    public ResponseWithData<?> uploadOfflineDataBatch(@RequestBody List<OfflineDto> offlineDtoList ) {

        try {
           String returnValue= offlineService.uploadOfflineData(offlineDtoList);
           if(returnValue.length()>1){
               return new ResponseWithData<>().success().code(200).msg("你有"+returnValue.substring(0,returnValue.length()-1)+"不在名下");
           }
            return new ResponseWithData<>().success().code(200).msg("上传成功");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     *  上传离线数据检测
     *
     * @param offlineDtoList
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/4/10 9:05 上午
     */
    @Log(title = "上传离线数据检测", businessType = BusinessType.INSERT)
    @PostMapping("uploadOfflineDataCheck")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "上传离线数据检测", httpMethod = "POST", notes = "上传离线数据")
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    @Transactional(rollbackFor = Exception.class)
    public ResponseWithData<?> uploadOfflineDataCheck(@RequestBody List<OfflineDto> offlineDtoList ) {
        try {
            ArrayList<String> offlineDataCheck= offlineService.uploadOfflineDataCheck(offlineDtoList);
            return new ResponseWithData<>().success(offlineDataCheck).code(200);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

}
